package de.koller.altitude.engine.entity.player;

import de.koller.altitude.input.AInput;
import de.koller.altitude.input.IKeyDownHandler;
import de.koller.altitude.input.IMouseMotionHandler;
import de.koller.oceanFramework.math.RVec3;
import org.lwjgl.input.Keyboard;

public class WalkingPlayerSteering implements IMouseMotionHandler, IKeyDownHandler {

	private final WalkingPlayer walkingPlayer;

	public WalkingPlayerSteering( WalkingPlayer walkingPlayer ) {
		this.walkingPlayer = walkingPlayer;
	}

	@Override
	public void keyDown( AInput i ) {
		int f = 0, s = 0;
		f -= i.isKeyDown( this, Keyboard.KEY_W ) ? 1 : 0;
		f += i.isKeyDown( this, Keyboard.KEY_S ) ? 1 : 0;
		s -= i.isKeyDown( this, Keyboard.KEY_A ) ? 1 : 0;
		s += i.isKeyDown( this, Keyboard.KEY_D ) ? 1 : 0;
		
		final float q = (float)Math.PI * .25f;
		float a = walkingPlayer.mo.location.alignment.y;
		if( f == -1 && s == -1 ) a += 5*q;
		if( f == -1 && s ==  0 ) a += 4*q;
		if( f == -1 && s ==  1 ) a += 3*q;
		if( f ==  0 && s == -1 ) a += 6*q;
		if( f ==  0 && s ==  0 ) return;
		if( f ==  0 && s ==  1 ) a += 2*q;
		if( f ==  1 && s == -1 ) a += 7*q;
		if( f ==  1 && s ==  0 ) a += 0*q;
		if( f ==  1 && s ==  1 ) a += 1*q;
		
		walkingPlayer.mo.movement.velocity.x = (float)(Math.sin( a ) * walkingPlayer.walkspeed.getValue());
		walkingPlayer.mo.movement.velocity.z = (float)(Math.cos( a ) * walkingPlayer.walkspeed.getValue());
	}
	
	@Override
	public void mouseMoved( int dx, int dy ) {
		RVec3 a = walkingPlayer.mo.location.alignment;
		a.y += -dx * 0.001;
		a.x += -dy * 0.001;
		
		a.y %= 2*Math.PI;
		if( a.x >  Math.PI / 2 ) a.x =  (float)Math.PI / 2;
		if( a.x < -Math.PI / 2 ) a.x = -(float)Math.PI / 2;
	}
	
}
